<?php

namespace app\channel\model;
use app\BaseModel;
use think\facade\Cache;
use think\Db;

use app\member\model\UsersModel;

//*------------------------------------------------------ */
//-- 代理会员表
/*------------------------------------------------------ */

class ProxyUsersModel extends BaseModel
{
    protected $table = 'channel_proxy_users';
    public $pk = 'user_id';
    protected $mkey = 'channel_proxy_user_mkey_';
    public $statusArr = ['0'=>'待审核','1'=>'审核通过','9'=>'帐号冻结'];
    public $regionProxy = ['0'=>'--','1'=>'省代','2'=>'市代',3=>'区代'];
    /*------------------------------------------------------ */
    //--  清除memcache
    /*------------------------------------------------------ */
    public function cleanMemcache($user_id)
    {
        Cache::rm($this->mkey . $user_id);
    }
    /**
     * 会员注册
     * @param array $inArr 写入数据
     * @param string $obj
     * @return bool|string
     */
    public function register($inArr = array(), &$obj = '')
    {
        Db::startTrans();
        $inArr['pid'] = empty($inArr['pid'])?0:$inArr['pid'] * 1;
        $UsersModel = new UsersModel();
        if ($inArr['user_id'] < 1){//没有会员ID先创建会员
            $inUserArr['mobile'] = $inArr['mobile'];
            $inUserArr['password'] = $inArr['password'];

            $inUserArr['pid'] = $inArr['pid'];
            $inUserArr['real_name'] = $inArr['real_name'];
            $inUserArr['is_channel'] = 1;
            $res = $UsersModel->register($inUserArr,0,true,$obj,false);
            if ($res !== true){
                Db::rollback();
                return $res;
            }
            $inArr['user_id'] = $UsersModel->userId;
        }else{
            $res = $UsersModel->upInfo($inArr['user_id'],['real_name'=>$inArr['real_name'],'is_channel'=>1]);
            if ($res < 1){
                Db::rollback();
                return '更新会员类型失败，请尝试重新提交.';
            }
        }
        unset($inArr['mobile']);
        $inArr['reg_time'] = time();
        $res = $this->create($inArr);
        if ($res < 1) {
            Db::rollback();
            return '创建代理帐号失败，请尝试重新提交.';
        }
        //推荐上级汇总处理
        $res = (new ProxyUsersSuperiorModel)->treat($inArr['user_id'], $inArr['pid'], false);
        if ($res == false) {
            Db::rollback();
            return '关联上级失败，请尝试重新提交.';
        }
        $obj->_log($inArr['user_id'], '代理注册', 'channel');
        Db::commit();
        return true;
    }
    /*------------------------------------------------------ */
    //-- 获取用户id
    //-- $field string 字段
    //-- $value string 查询内容
    /*------------------------------------------------------ */
    public function getUserIdByField($field,$value)
    {
        if ($field == 'mobile'){
            $where[] = ['mobile','=',$value];
            $user_id = (new UsersModel)->where($where)->value('user_id');
            if ($user_id < 1){
                return '没找到相关"手机号码：'.$value.'"代理信息.';
            }
        }elseif ($field == 'warrant_number'){
            $where[] = ['warrant_number','=',$value];
            $user_id = $this->where($where)->value('user_id');
            if ($user_id < 1){//查询历史授权记录中是否存在
                $user_id = (new ProxyWarrantLogModel)->where($where)->value('user_id');
            }
            if ($user_id < 1){
                return '没找到相关"授权码：'.$value.'"代理信息.';
            }
        }elseif ($field == 'id_card_number'){
            $where[] = ['id_card_number','=',$value];
            $user_id = (new UsersModel)->where($where)->value('user_id');
            if ($user_id < 1){
                return '没找到相关"身份证号码：'.$value.'"代理信息.';
            }
        }else{
            return '发生错误，没有相关查询定义.';
        }
        return $user_id;
    }
    /*------------------------------------------------------ */
    //-- 获取用户信息
    //-- $user_id 查询值
    //-- $type 查询类型
    //-- $isCache 是否调用缓存
    /*------------------------------------------------------ */
    public function info($user_id,  $isCache = true)
    {
        if (empty($user_id)) return [];
        if ($isCache == true) $info = Cache::get($this->mkey.$user_id);
        if (empty($info) == true){
            $info = $this->where('user_id', $user_id)->find();
            if (empty($info)) {
                return [];
            }
            $info = $info->toArray();
            $RegionModel = new \app\mainadmin\model\RegionModel();
            $info['byUser'] = (new UsersModel)->where('user_id',$info['user_id'])->find()->toArray();
            $info['region_text'] = '';
            if ($info['byUser']['district'] > 0){
                $region = $RegionModel->info($info['byUser']['district']);
                $info['region_text'] = $region['merger_name'];
            }
            $info['bank_region_text'] = '';
            if ($info['bank_city'] > 0){
                $region = $RegionModel->info($info['bank_city']);
                $info['bank_region_text'] = $region['merger_name'];
            }

            if ($info['supply_uid'] == 0){
                $info['supply_name'] = '厂家';
                $info['parent_level'] = '厂家';
                $info['supply_headimgurl'] = settings('logo');
                $info['supply_mobile'] = settings('channel_mobile');
            }else{
                $parentUser = $this->info($info['supply_uid']);
                $info['supply_name'] = $parentUser['byUser']['real_name'];
                $info['parent_level'] = $parentUser['proxyLevel']['proxy_name'];
                $info['supply_headimgurl'] = $parentUser['byUser']['headimgurl'];
                $info['supply_mobile'] = $parentUser['byUser']['mobile'];
            }
            Cache::set($this->mkey.$user_id,$info, 30);
        }
        unset($info['byUser']['password'],$info['byUser']['pay_password']);
        $proxyLevel = (new ProxyLevelModel)->getList();
        if (empty($proxyLevel[$info['proxy_id']]) == false){
            $info['proxyLevel'] = $proxyLevel[$info['proxy_id']];
        }else{
            $info['proxyLevel']['proxy_id'] = -1;
            $info['proxyLevel']['proxy_name'] = '无身份';
        }
        $info['account'] = (new WalletModel)->getWalletInfo($user_id,false);//获取代理会员帐户信息
        return $info;
    }

    /*------------------------------------------------------ */
    //-- 验证是否满足加入门槛
    //-- $userInfo 代理信息
    /*------------------------------------------------------ */
    public function joinCondition(&$userInfo)
    {
        if ($userInfo['join_condition'] == 1){
            return $userInfo;
        }
        if ($userInfo['proxyLevel']['is_join_limit'] == 0){//没开启加入门槛
            $userInfo['join_condition'] = 1;
            $upData['join_condition'] = 1;
            if ($userInfo['proxyLevel']['is_auto_check'] == 1){//自动审核
                $upData['status'] = 1;
            }
            $this->upInfo($userInfo['user_id'], $upData);//直接更新代理已满足条件
            return $userInfo;
        }
        $join_condition = true;
        $RechargeLogModel = new RechargeLogModel();
        if ($userInfo['proxyLevel']['join_uplevel_goods_money_limit'] == 1){
            //是否满足充值升级货款
            if ($userInfo['account']['uplevel_goods_money_total']>=$userInfo['proxyLevel']['uplevel_goods_money']){
                $userInfo['join_uplevel_goods_money_ok'] = 1;
            }else{
                $userInfo['join_uplevel_goods_money_ok'] = 0;
                $join_condition = false;
                $where = [];
                $where[] = ['user_id','=',$userInfo['user_id']];
                $where[] = ['type','=','uplevelGoodsMoney'];
                $where[] = ['order_amount','=',$userInfo['proxyLevel']['uplevel_goods_money']];
                $rechargeLog = $RechargeLogModel->where($where)->order('order_id DESC')->find();
                if (empty($rechargeLog) == false){
                    if ($rechargeLog['status'] == 0){
                        $userInfo['join_uplevel_goods_money_ok'] = 2;
                    }elseif ($rechargeLog['status'] == 1){
                        $userInfo['join_uplevel_goods_money_ok'] = 3;
                    }
                }
            }
        }
        if ($userInfo['proxyLevel']['join_earnest_money_limit'] == 1){
            //查询保证金是否满足要求
            if ($userInfo['account']['earnest_money']>=$userInfo['proxyLevel']['join_earnest_money']){
                $userInfo['join_earnest_money_ok'] = 1;
            }else{
                $userInfo['join_earnest_money_ok'] = 0;
                $join_condition = false;
                $where = [];
                $where[] = ['user_id','=',$userInfo['user_id']];
                $where[] = ['type','=','earnestMoney'];
                $where[] = ['status','<',9];
                $rechargeLog = $RechargeLogModel->where($where)->order('order_id DESC')->find();
                if (empty($rechargeLog) == false){
                    if ($rechargeLog['status'] == 0){
                        $userInfo['join_earnest_money_ok'] = 2;
                    }elseif ($rechargeLog['status'] == 1){
                        $userInfo['join_earnest_money_ok'] = 3;
                    }
                }
            }
        }
        if ($userInfo['proxyLevel']['join_goods_money_limit'] == 1){
            //查询充值货款是否满足要求
            if ($userInfo['account']['goods_money']+$userInfo['account']['goods_money_outlay_total']>=$userInfo['proxyLevel']['join_goods_money']){
                $userInfo['join_goods_money_ok'] = 1;
            }else{
                $join_condition = false;
                $userInfo['join_goods_money_ok'] = 0;
                $where = [];
                $where[] = ['user_id','=',$userInfo['user_id']];
                $where[] = ['type','=','goodsMoney'];
                $where[] = ['status','<',9];
                $rechargeLog = $RechargeLogModel->where($where)->order('order_id DESC')->find();
                if (empty($rechargeLog) == false){
                    if ($rechargeLog['status'] == 0){
                        $userInfo['join_goods_money_ok'] = 2;
                    }elseif ($rechargeLog['status'] == 1){
                        $userInfo['join_goods_money_ok'] = 3;
                    }
                }
            }
        }
        $OrderModel = new OrderModel();
        if ($userInfo['proxyLevel']['order_first_spot_limit'] == 1){
            $userInfo['order_first_spot_ok'] = 0;
            $where = [];
            //限制首单 - 现货
            $where[] = ['user_id','=',$userInfo['user_id']];
            $where[] = ['purchase_type','=',2];
            $where[] = ['pay_status','=',1];
            $count = $OrderModel->where($where)->count();
            if ($count > 0){
                $userInfo['order_first_spot_ok'] = 1;
            }else{
                $join_condition = false;
                $where = [];
                $where[] = ['user_id','=',$userInfo['user_id']];
                $where[] = ['purchase_type','=',2];
                $where[] = ['order_status','=',0];
                $count = $OrderModel->where($where)->count();
                if ($count > 0){
                    $userInfo['order_first_spot_ok'] = 2;
                }
            }
        }
        if ($userInfo['proxyLevel']['order_first_cloud_limit'] == 1){
            $userInfo['order_first_cloud_ok'] = 0;
            $where = [];
            //限制首单 - 云仓
            $where[] = ['user_id','=',$userInfo['user_id']];
            $where[] = ['purchase_type','=',1];
            $where[] = ['pay_status','=',1];
            $count = $OrderModel->where($where)->count();
            if ($count > 0){
                $userInfo['order_first_cloud_ok'] = 1;
            }else{
                $join_condition = false;
                $where = [];
                $where[] = ['user_id','=',$userInfo['user_id']];
                $where[] = ['purchase_type','=',1];
                $where[] = ['order_status','=',0];
                $count = $OrderModel->where($where)->count();
                if ($count > 0){
                    $userInfo['order_first_cloud_ok'] = 2;
                }
            }
        }
        if ($join_condition == true){//全条件满足
            $userInfo['join_condition'] = 1;
            $upData['join_condition'] = 1;
            if ($userInfo['proxyLevel']['is_auto_check'] == 1){//自动审核
                $upData['status'] = 1;
            }
            $this->upInfo($userInfo['user_id'], $upData);//直接更新代理已满足条件
        }
       return $userInfo;
    }

    /*------------------------------------------------------ */
    //-- 更新会员信息
    /*------------------------------------------------------ */
    public function upInfo($user_id, $data)
    {
        $user_id = $user_id * 1;
        $data['update_time'] = time();
        $res = $this->where('user_id', $user_id)->update($data);
        $this->cleanMemcache($user_id);
        return $res;
    }
    /*------------------------------------------------------ */
    //-- 获取代理的上级关联链
    /*------------------------------------------------------ */
    public function getSuperiorList($user_id = 0)
    {
        if ($user_id < 1) return array();
        $chain = Cache::get('proxyUserSuperior_' . $user_id);
        if ($chain) return $chain;
        $proxyLevel = (new ProxyLevelModel)->getList();
        $i = 1;
        $user_id = $this->where('user_id', $user_id)->value('pid');
        if ($user_id < 1) return [];
        do {
            $info = $this->info($user_id);
            $chain[$i]['level'] = $i;
            $chain[$i]['user_id'] = $info['user_id'];
            $chain[$i]['reg_time'] = dateTpl($info['reg_time']);
            $chain[$i]['real_name'] = empty($info['byUser']['real_name']) ? '未填写' : $info['byUser']['real_name'];
            $chain[$i]['proxy_name'] = $info['proxy_id'] > 0 ? $proxyLevel[$info['proxy_id']]['proxy_name'] : '无身份';
            $user_id = $info['pid'];
            $i++;
        } while ($user_id > 0);

        Cache::set('proxyUserSuperior_' . $user_id, $chain, 300);
        return $chain;
    }
    /*------------------------------------------------------ */
    //-- 获取拿货上级
    //-- $proxy_id intval 代理数据
    //-- $pid intval 上级ID
    //-- $proxyLevel array 代理层级
    /*------------------------------------------------------ */
    public function getPurchaseUid($proxy_id,$pid,&$proxyLevel){
        if ($pid == 0) return $pid;
        $pUserInfo = $this->info($pid);
        if ($proxy_id == $pUserInfo['proxy_id']){//相同继续向上找
            $pid = $this->getPurchaseUid($proxy_id,$pUserInfo['pid'],$proxyLevel);
        }elseif ($proxyLevel[$proxy_id]['level'] < $proxyLevel[$pUserInfo['proxy_id']]['level']){//上级层级低，继续向上找
            $pid = $this->getPurchaseUid($proxy_id,$pUserInfo['pid'],$proxyLevel);
        }
        return $pid;
    }


    /*------------------------------------------------------ */
    //-- 获取邀请海报，每张海报小时重新生成
    //-- $user_id int 代理会员ID
    //-- $proxy_id int 邀请的层级
    /*------------------------------------------------------ */
    public function getInvitePoster($user_id,$proxy_id){
        $file_path = config('config._upload_') .'snap_file/invite/'.date('d').'/';
        makeDir($file_path);
        $userInfo = $this->info($user_id);
        $file =  $file_path.$user_id.'_'.$proxy_id.'_'.date('H').'.jpg';
        $return['file'] = trim($file,'.');
        $return['url'] = settings("channel_web_url") . '/pagesB/channel/passport/register?share_token=' . $userInfo['byUser']['token'] . '&proxy_id=' . $proxy_id;
        if (file_exists($file) == false) {
            $temp = (new LicenseTempModel)->where('type', 2)->find();
            if (empty($temp)) {
                return '未设置邀请海报.';
            }

            include EXTEND_PATH . 'phpqrcode/phpqrcode.php';//引入PHP QR库文件
            $QRcode = new \phpqrcode\QRcode();
            $qrfile = $file_path .$userInfo['byUser']['token'].'_'.$proxy_id.'.png';
            $QRcode::png($return['url'], $qrfile, "L", 10, 1, 2, true);
            $content = htmlspecialchars_decode($temp['content']);
            $content = json_decode($content, true);
            $select_dom['avatar'] = ['title' => '头像', 'img' => '.' . $userInfo['byUser']['headimgurl']];
            $select_dom['qrcode'] = ['title' => '二维码', 'img' => $qrfile];
            $select_dom['warrant_number'] = ['title' => '授权码', 'text' => $userInfo['warrant_number']];
            $select_dom['real_name'] = ['title' => '姓名', 'text' => $userInfo['byUser']['real_name']];
            $select_dom['mobile'] = ['title' => '手机号码', 'text' => $userInfo['byUser']['mobile']];
            $select_dom['id_card_number'] = ['title' => '身份证号码', 'text' => $userInfo['byUser']['id_card_number']];
            $select_dom['proxy_level'] = ['title' => '授权级别', 'text' => $userInfo['proxyLevel']['proxy_name']];
            $select_dom['warrant_end_time'] = ['title' => '授权有效时间', 'text' => dateTpl($userInfo['warrant_end_time'], 'Y-m-d H:i:s', true)];
            $proxyLevel = (new ProxyLevelModel)->getList();
            $select_dom['invite_level'] = ['title' => '邀请您成为', 'text' => $proxyLevel[$proxy_id]['proxy_name']];
            foreach ($content as $key => $arr) {
                $arr['info'] = $select_dom[$key];
                $content[$key] = $arr;
            }
            $data['content'] = $content;
            $data['temp_bg'] = $temp['temp_bg'];
            $MergeImg = new \lib\MergeImg();
            $MergeImg->licenseImg($data, $file);
            @unlink($qrfile);
        }
        return $return;
    }
    /*------------------------------------------------------ */
    //-- 获取授权证书，每张海报每小时重新生成
    //-- $userInfo array 代理会员信息
    /*------------------------------------------------------ */
    public function getAuthorizedImg(&$userInfo){
        $file_path = config('config._upload_') .'snap_file/authorized/'.date('d').'/';
        makeDir($file_path);
        $file =  $file_path.$userInfo['user_id'].'_'.date('H').'.jpg';
        $return['file'] = trim($file,'.');
        $return['url'] = settings("channel_web_url") . '/pagesB/channel/center/authorizedShow?warrant_number=' . $userInfo['warrant_number'];
        if (file_exists($file) == false) {
            $where = [];
            $where[] = ['type','=',1];
            $where[] = ['is_default','=',1];
            $temp = (new LicenseTempModel)->where($where)->find();
            if (empty($temp)) {
                return false;
            }
            include EXTEND_PATH . 'phpqrcode/phpqrcode.php';//引入PHP QR库文件
            $QRcode = new \phpqrcode\QRcode();
            $qrfile = $file_path .$userInfo['warrant_number'].'.png';
            $QRcode::png($return['url'], $qrfile, "L", 10, 1, 2, true);
            $content = htmlspecialchars_decode($temp['content']);
            $content = json_decode($content, true);
            $select_dom['avatar'] = ['title' => '头像', 'img' => '.' . $userInfo['byUser']['headimgurl']];
            $select_dom['qrcode'] = ['title' => '二维码', 'img' => $qrfile];
            $select_dom['warrant_number'] = ['title' => '授权码', 'text' => $userInfo['warrant_number']];
            $select_dom['real_name'] = ['title' => '姓名', 'text' => $userInfo['byUser']['real_name']];
            $select_dom['mobile'] = ['title' => '手机号码', 'text' => $userInfo['byUser']['mobile']];
            $select_dom['id_card_number'] = ['title' => '身份证号码', 'text' => $userInfo['byUser']['id_card_number']];
            $select_dom['proxy_level'] = ['title' => '授权级别', 'text' => $userInfo['proxyLevel']['proxy_name']];
            $select_dom['warrant_end_time'] = ['title' => '授权有效时间', 'text' => dateTpl($userInfo['warrant_end_time'], 'Y-m-d H:i:s', true)];
            foreach ($content as $key => $arr) {
                $arr['info'] = $select_dom[$key];
                $content[$key] = $arr;
            }
            $data['content'] = $content;
            $data['temp_bg'] = $temp['temp_bg'];
            $MergeImg = new \lib\MergeImg();
            $MergeImg->licenseImg($data, $file);
            @unlink($qrfile);
        }
        return $return;
    }
    /*------------------------------------------------------ */
    //-- 升级处理
    //-- $user_id int 会员ID
    //-- $proxy_id int 代理要升级层级ID
    /*------------------------------------------------------ */
    public function upgrade($user_id,$proxy_id,$log='',&$obj){
        $userInfo = $this->where('user_id',$user_id)->find();
        Db::startTrans();//开启事务
        $inArr['end_time'] = strtotime($obj->warrant_end_time);
        $inArr['user_id'] = $user_id;
        $inArr['proxy_id'] = $proxy_id;
        $inArr['before_proxy_id'] = $userInfo['proxy_id'];
        $inArr['operate_uid'] = AUID;
        $inArr['add_time'] = time();
        $inArr['warrant_number'] = date('ymdhis').rand(100000,999999);
        $res = (new ProxyWarrantLogModel)->save($inArr);
        if ($res < 1){
            Db::rollback();//回滚事务
            return '写入授权失败，请重试.';
        }

        $ProxyLevelList = (new ProxyLevelModel)->getList();
        $upData['status'] = 1;
        $upData['proxy_id'] = $proxy_id;
        $upData['before_proxy_id'] = $userInfo['proxy_id'];
        $upData['warrant_number'] = $inArr['warrant_number'];
        if (empty($obj->warrant_end_time)){
            $upData['warrant_end_time'] = strtotime("+1 year");
        }else{
            $upData['warrant_end_time'] = $inArr['end_time'];
        }
        //当前项目需要。正常去除 $ProxyLevelList[$proxy_id]['level'] > 1 条件
        if ($userInfo['uplevel_time'] == 0 || ($userInfo['proxy_id'] != $proxy_id && $ProxyLevelList[$proxy_id]['level'] > 1)){
            $upData['uplevel_time'] = time();
        }
        if ($userInfo['proxy_id'] != $proxy_id){//层级变化, 判断是否更新上级供货商
            $upData['supply_uid'] = $this->getSupplyUid($user_id,$proxy_id,$ProxyLevelList);
        }
        $upData['join_condition'] = 1;//授权即设为满足加入条件
        $res = $this->upInfo($user_id,$upData);
        if ($res < 1){
            Db::rollback();//回滚事务
            return '更新代理数据失败.';
        }
        $log .= '，代理层级：'.$ProxyLevelList[$proxy_id]['proxy_name'];
        if ($upData['supply_uid'] != $userInfo['supply_uid']){
           $log .= '，供货上级ID调整为：'.$upData['supply_uid'].'，原供货上级ID：'.$userInfo['supply_uid'];
        }else{
           $log .= '，供货上级不调整.';
        }
        $obj->_log($user_id, $log, 'channel');
        $this->cleanMemcache($user_id);
        if ($userInfo['proxy_id'] != $proxy_id){
            $res = $this->resetSupplyBind($user_id,$obj);
            if ($res !== true){
                Db::rollback();//回滚事务
                return $res;
            }
        }
        Db::commit();//提交事务
        return true;
    }
    /*------------------------------------------------------ */
    //-- 获取上级供货代理
    //-- $user_id int 供货代理ID
    //-- $up_proxy_id int 代理要升级层级ID
    //-- $ProxyLevelList array 代理层级
    //-- $pid int 所属推荐上级，修改推荐上级时传值，其它查询当前数据获取
    /*------------------------------------------------------ */
    public function getSupplyUid($user_id,$up_proxy_id,&$ProxyLevelList,$pid=-1){
        if ($ProxyLevelList[$up_proxy_id]['supply_pid'] == 0){
            return 0;//厂家出货
        }
        if ($pid == 0){
            return 0;//厂家出货
        }
        if ($pid < 0){
            $userInfo = $this->where('user_id',$user_id)->field('pid,proxy_id')->find();
            if ($userInfo['pid'] == 0){
                return 0;//厂家出货
            }
            $pid = $userInfo['pid'];
        }
        $upLevel = $ProxyLevelList[$up_proxy_id]['level'];
        $supply_uid = 0;
        do{
            $pUserInfo = $this->where('user_id',$pid)->field('user_id,pid,proxy_id,status')->find();
            if ($ProxyLevelList[$pUserInfo['proxy_id']]['level'] < $upLevel && $pUserInfo['status'] == 1){
                $supply_uid = $pUserInfo['user_id'];
                break;
            }
           $pid = $pUserInfo['pid'];
        }while($pid > 0);
        return $supply_uid;
    }

    /*------------------------------------------------------ */
    //-- 重置供货关系
    //-- $user_id int 供货代理ID
    /*------------------------------------------------------ */
    public function resetSupplyBind($user_id,&$obj){
        $ProxyUsersSuperiorModel = new ProxyUsersSuperiorModel();
        $where[] = ['','exp',Db::raw("FIND_IN_SET('".$user_id."',superior)")];
        $user_ids = $ProxyUsersSuperiorModel->where($where)->column('user_id');//获取所有下级
        $ProxyLevelList =  (new ProxyLevelModel)->getList();
        foreach ($user_ids as $uid){
            if ($uid == $user_id){
                continue;
            }
            $userInfo = $this->find($uid);
            $supply_uid  = $this->getSupplyUid($uid,$userInfo['proxy_id'],$ProxyLevelList);
            if ($supply_uid == $userInfo['supply_uid']){
                continue;//不需要修改
            }
            $upData['supply_uid'] = $supply_uid;
            $res = $this->where('user_id',$uid)->update($upData);
            if ($res < 1){
                return '重置供货关系失败-'.$uid.'-'.$supply_uid;
            }
            $obj->_log($uid, '上级层级变化，重置供货关系.', 'channel');
            $this->cleanMemcache($uid);
        }
        return true;
    }

}
